<?php
/**
 * Created by PhpStorm.
 * User: moon
 * Date: 2021/6/8
 * Time: 15:34
 */

namespace app\admin\controller;


use app\AdminRest;
use think\App;
use think\facade\Cache;
use think\facade\View;
use app\admin\model\Admin as AdminModel;

class Admin extends AdminRest
{

    protected $model;

    public function __construct(App $app, AdminModel $aModel)
    {
        parent::__construct($app);
        $this->model = $aModel;
    }

    public function index()
    {
        return View::fetch('index');
    }

    public function adminList()
    {
        $admins = $this->model->where(['deleted'=>0])->paginate($this->_param['size'] ?? 10);
        return $this->success($admins);
    }

    public function editPsw()
    {
        switch ($this->_method) {
            case  'GET';
                return View::fetch('editPsw', ['editId' => $this->_param['aid']]);
                break;
            case 'POST';
                if (!isset($this->_param['aid']) || empty($this->_param['aid']))
                    return $this->error('信息缺失');
                if (!isset($this->_param['oldPsw']) || $this->_param['oldPsw'] != $this->_admin['passwd'])
                    return $this->error('原密码错误');
                if (!isset($this->_param['newPsw']) || !isset($this->_param['newPsw1']) || $this->_param['newPsw1'] != $this->_param['newPsw'])
                    return $this->error('新密码不一致或有未填写');
                $this->model->where(['admin_id' => $this->_param['aid']])->save(['passwd' => $this->_param['newPsw1']]);
                if ($this->_admin['admin_id'] == $this->_param['aid']) {
                    Cache::delete($this->_token);
                    $isOwn = 1;
                }
                return $this->success(['is_own' => $isOwn ?? 0]);
                break;
        }

    }

    public function editAdmin()
    {
        if (!isset($this->_param['aid']) || !isset($this->_param['type']))
            return $this->error('数据缺失');
        switch ($this->_param['type']) {
            case 'del':
                $data = ['deleted' => 1];
                break;
            case 'open':
                $data = ['status' => 1];
                break;
            case 'close':
                $data = ['status' => 2];
                break;
        }
        $this->model->where(['admin_id' => $this->_param['aid']])->save($data);
        return $this->success('success');
    }

    public function addAdmin()
    {
        switch ($this->_method) {
            case  'GET';
                return View::fetch('addAdmin');
                break;
            case 'POST':
                if (!isset($this->_param['account']) || !isset($this->_param['psw']))
                    return $this->error('用户名或密码缺失');
                $this->model->save([
                    'account' => $this->_param['account'],
                    'passwd' => $this->_param['psw'],
                    'create_time' => time()
                ]);
                return $this->success('添加成功');
                break;

        }

    }
}